package com.mobile.liuyi_s.orderlunch.fragment;


import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobile.liuyi_s.orderlunch.MainActivity;
import com.mobile.liuyi_s.orderlunch.MyApplication;
import com.mobile.liuyi_s.orderlunch.R;
import com.mobile.liuyi_s.orderlunch.activity.ChangePwdActivity;
import com.mobile.liuyi_s.orderlunch.entity.MyUser;

import cn.bmob.v3.BmobUser;

/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends Fragment implements View.OnClickListener{

    private Toolbar mToolbar;
    private String currUserName;

    private TextView defaultUserName;
    private ImageView defaultUserIcon;
    private Button mButtonChangePwd;

    public MeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_me, container, false);
        MyUser user = BmobUser.getCurrentUser(MyUser.class);
        currUserName = "姓名："+user.getUsername();
        initView(view);
        defaultUserName.setText(currUserName);
        return view;
    }
    private void initView (View itemView) {
        initToolbar(itemView);
        defaultUserName = (TextView) itemView.findViewById(R.id.txt_default_user_name_me_fragment);
        defaultUserIcon = ((ImageView) itemView.findViewById(R.id.img_default_user_icon_me_fragment));
        mButtonChangePwd = ((Button) itemView.findViewById(R.id.btn_change_pwd_me_fragment));
        mButtonChangePwd.setOnClickListener(this);
    }

    private void initToolbar(View itemView) {
        mToolbar = ((Toolbar) itemView.findViewById(R.id.toolbar_me_fragment));
        mToolbar.setTitle("");
        setHasOptionsMenu(true);
        ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.me_fragment_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.logout_menu:
                showDialog();
                break;
        }
        return true;
    }

    private void showDialog() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
        dialog.setTitle("警告");
        dialog.setMessage("您确定要退出吗？");
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
                MyUser.logOut();
                Intent intent = new Intent(getContext(), MainActivity.class);
                intent.putExtra("finish", true);
                startActivity(intent);
//                android.os.Process.killProcess(android.os.Process.myPid());
//                System.exit(0);

            }
        });
        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {

            }
        });
        dialog.show();
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_change_pwd_me_fragment:
                Intent intent = new Intent(getContext(), ChangePwdActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

}
